﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Blog.Models.UserServices
{
    public class UserService:IUserService
    {
        DataDataContext db=new DataDataContext();
        public IRoleService RoleService=new RoleService();
        public AddResult Register(User user)
        {
            throw new System.NotImplementedException();
        }

        public void Update(User user)
        {
            throw new System.NotImplementedException();
        }

        public bool Exist(string username)
        {
            throw new System.NotImplementedException();
        }

        public LoginResult Login(string username, string password)
        {

            User user = (from c in db.Users
                         where (c.UserName == username && c.PassWord == password)
                         select c).FirstOrDefault();
            if (user != null)
            {
                List<string> roles = RoleService.GetRoleByUserId(user.UserId).Select(c => c.RoleName.ToLower()).ToList();

                return new LoginResult
                {
                    IsSuccess = true,
                    User = user,
                    Roles = roles
                };
            }

            return new LoginResult { IsSuccess = false };
        }

        public bool Authenticate(string username, string password)
        {
            throw new System.NotImplementedException();
        }

        public IList<Role> GetRoleByUserId(int userId)
        {
            return RoleService.GetRoleByUserId(userId);
        }

        public User GetUserByUsername(string username)
        {
            throw new System.NotImplementedException();
        }

        public bool HasRole(int userId, string roleName)
        {
            throw new System.NotImplementedException();
        }

        public string EncryptPassword(string password)
        {
            throw new System.NotImplementedException();
        }

        public IList<User> GetAllUser()
        {
            throw new System.NotImplementedException();
        }

        public IList<Role> Quyen(int id, int index, int size)
        {
            throw new System.NotImplementedException();
        }

        public void ChangePassword(string username, string password)
        {
            throw new System.NotImplementedException();
        }

        public int DeleteUser(int userId)
        {
            throw new System.NotImplementedException();
        }

        public User GetById(int userId)
        {
            throw new System.NotImplementedException();
        }

        public IList<User> GetByDepartment(string departmentName)
        {
            throw new System.NotImplementedException();
        }
    }
}
